package net.wicp.tams.common.binlog.dump.handlerConsumer;

import java.util.HashMap;
import java.util.Map;

import com.lmax.disruptor.WorkHandler;

import lombok.extern.slf4j.Slf4j;
import net.wicp.tams.common.apiext.LoggerUtil;
import net.wicp.tams.common.binlog.dump.bean.Dump;
import net.wicp.tams.common.binlog.dump.bean.DumpEvent;
import net.wicp.tams.common.binlog.dump.listener.IBusiFilter;
import net.wicp.tams.common.constant.JvmStatus;

@Slf4j
public class BusiHander implements WorkHandler<DumpEvent> {

	private final Map<String, IBusiFilter<DumpEvent>> filtermap = new HashMap<String, IBusiFilter<DumpEvent>>();
	private final String formatestr = "%s.%s";

	public BusiHander(Dump[] dumps) {
		for (Dump dump : dumps) {
			filtermap.put(String.format(formatestr, dump.getDb(), dump.getTb()), dump.getBusiFilter());
		}
	}

	@Override
	public void onEvent(final DumpEvent event) throws Exception {
		Thread.currentThread().setName("BusiHanderThread");
		IBusiFilter<DumpEvent> iBusiFilter = this.filtermap
				.get(String.format(formatestr, event.getDump().getDb(), event.getDump().getTb()));
		if (iBusiFilter == null) {
			return;
		}
		try {
			iBusiFilter.doBusi(event);
		} catch (Throwable e) {
			log.error("业务处理失败", e);// TODO
			LoggerUtil.exit(JvmStatus.s15);
		}
	}
}
